﻿	      <?php include("conn.php");
             mysql_query('SET NAMES UTF8'); 

	  

class MyPrizeBean
{
	public $title;
	public $price;
	public $images;
	public $yuanjia;
	public $goodsid;
	public $reply;
	public $xihuan;
	public $summary;
   public $distance;
	function __construct($title,$price,$images,$yuanjia,$goodsid,$reply,$xihuan,$summary,$distance)
	{
		$this->title      = $title;
		$this->price = $price;
		$this->images     = $images;
		$this->yuanjia      = $yuanjia;
		$this->goodsid    = $goodsid;
		$this->reply    = $reply;
		$this->xihuan    = $xihuan;
		$this->summary    = $summary;
		$this->distance    = $distance;
	}
}

if($_REQUEST['page']!="")
{
$de=$_REQUEST['page']*$_REQUEST['pagesize'];
$size=$_REQUEST['pagesize'];
}

else
{
$de=0;
$size=5;
}
$prize = array();
$rert2=mysql_query("select * from user where  userid='".$_REQUEST[userid]."'"); 
          $user1=mysql_fetch_array($rert2);
//最近函数
define(EARTH_RADIUS, 6371);//地球半径，平均半径为6371km
 /**
 *计算某个经纬度的周围某段距离的正方形的四个点
 *
 *@param lng float 经度
 *@param lat float 纬度
 *@param distance float 该点所在圆的半径，该圆与此正方形内切，默认值为0.5千米
 *@return array 正方形的四个点的经纬度坐标
 */
 function returnSquarePoint($lng, $lat,$distance = 2){
 
    $dlng =  2 * asin(sin($distance / (2 * EARTH_RADIUS)) / cos(deg2rad($lat)));
    $dlng = rad2deg($dlng);
     
    $dlat = $distance/EARTH_RADIUS;
    $dlat = rad2deg($dlat);
     
    return array(
                'left-top'=>array('lat'=>$lat + $dlat,'lng'=>$lng-$dlng),
                'right-top'=>array('lat'=>$lat + $dlat, 'lng'=>$lng + $dlng),
                'left-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng - $dlng),
                'right-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng + $dlng)
                );
 }
//使用此函数计算得到结果后，带入sql查询。
$squares = returnSquarePoint($user1[longitude],$user1[latitude]);
//最近结束
   function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) {//计算距离
    $theta = $longitude1 - $longitude2;
    $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
    $miles = acos($miles);
    $miles = rad2deg($miles);
    $miles = $miles * 60 * 1.1515;
    $kilometers = $miles * 1.609344;
    return compact('kilometers'); 

}//计算距离 end
$query = mysql_query("select * from `goods` where latitude<>0 and latitude>{$squares['right-bottom']['lat']} and latitude<{$squares['left-top']['lat']} and longitude>{$squares['left-top']['lng']} and longitude<{$squares['right-bottom']['lng']} limit $de,$size");

while($rowf=mysql_fetch_array($query))
{

    
	   $longitude1=$rowf1['longitude'];$latitude1=$rowf1['latitude'];$longitude2=$rowf['longitude'];$latitude2=$rowf['latitude'];
	   
	


$distance = getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2);
foreach ($distance as $unit => $value) {
    $dic= number_format($value,1);
}

	
	

 $rert36=mysql_query("select *from goods where goodsid='".$rowf[goodsid]."'");
     $rowf6=mysql_fetch_array($rert36);
	 
	 $ime=$rowf6[images];
				$de = array();
				$de=explode("," ,$ime);
				$images=$de[0];
				
				 if(strlen($rowf6['title'])>14)
		
		{
		
		$titles=mb_substr($rowf6['title'], 0,14,"UTF-8");
		}
		else
		{
		$titles=$rowf6['title'];
		}
		
		if(strlen($rowf6['summary'])>30)
		
		{
		
		$summarys=mb_substr($rowf6['summary'], 0,30,"UTF-8");
		}
		else
		{
		$summarys=$rowf6['summary'];
		}
		

			$prize[] = new MyPrizeBean($titles,$rowf6['price'],$images,$rowf6['yuanjia'],$rowf6['goodsid'],$rowf6['replynumber'],$rowf6['xihuan'],$summarys,$dic);
		
	}

echo json_encode($prize);


?>